using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace xna_game_engine.source
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class LootManager
    {
        protected List<LootPile> mLootPiles;

        // Functions
        public LootManager()
        {
            mLootPiles = new List<LootPile>();
            //mLookupTable = new List<Point>();
            //mLootTables = new Dictionary<int, List<BaseItem>>();
            //for (int i = 0; i < 10; i++)
            //{
            //    mLootTables[i] = new List<BaseItem>();
            //}
            //mLootTables[0].Add(new BaseItem("Rusty Axe"));
            //mLootTables[0].Add(new BaseItem("Rusty Sword"));
            //mLootTables[0].Add(new BaseItem("Rusty Knife"));
            //mLootTables[0].Add(new BaseItem("Rusty Dagger"));
            //mLootTables[0].Add(new BaseItem("Rusty Bone"));
            //mLookupTable.Add(new Point(1, 0));
            //mLookupTable.Add(new Point(2, 0));
            //mLookupTable.Add(new Point(3, 0));
            //mLookupTable.Add(new Point(4, 0));
            // Load items from file
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            for (int i = 0; i < mLootPiles.Count; i++)
            {
                if (mLootPiles[i].IsLooted)
                {
                    mLootPiles.RemoveAt(i);
                }
            }
        }

        public void CheckCollisions(BaseCharacter player)
        {
            for (int i = 0; i < mLootPiles.Count; i++)
            {
                if (player.Bounds.Intersects(mLootPiles[i].Bounds))
                {
                    foreach (BaseItem item in mLootPiles[i].Items)
                    {
                        player.AddItemToInventory(item);
                        Console.WriteLine("" + player.Name + " has picked up a " + item.Name + "!");
                    }
                    if (mLootPiles[i].Gold > 0)
                    {
                        player.Gold += mLootPiles[i].Gold;
                        Console.WriteLine("" + player.Name + " has picked up " + mLootPiles[i].Gold + " Gold!");
                    }
                    mLootPiles.RemoveAt(i);
                }
            }
        }

        public void AddLootPile(LootPile loots)
        {
            mLootPiles.Add(loots);
        }
        public void Draw()
        {
            foreach (LootPile loot in mLootPiles)
            {
                loot.Draw();
            }
        }
    }
}